package com.zz.parkingsystem.controller.admin;

import com.zz.parkingsystem.dto.CarDTO;
import com.zz.parkingsystem.dto.OrderDTO;
import com.zz.parkingsystem.dto.ParkDTO;
import com.zz.parkingsystem.dto.UserDTO;
import com.zz.parkingsystem.entity.Parking;
import com.zz.parkingsystem.service.PCUserService;
import com.zz.parkingsystem.util.ObjectExcelView;
import com.zz.parkingsystem.util.PageData;
import com.zz.parkingsystem.util.Result;
import com.zz.parkingsystem.util.ResultGenerator;
import com.zz.parkingsystem.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/user")
@Api(tags = "展示用户信息")
public class AdminUserController {
    @Resource
    private PCUserService pcUserService;

    @ApiOperation(value = "展示用户详细列表",notes = "根据昵称，手机号，微信是否绑定，状态进行模糊查询")
    @PostMapping("/getAllUser")
    public Result getAllUser(@RequestBody UserDTO userDto){
        userDto.setStartPage((userDto.getCurrentPage()-1)* userDto.getPageSize());
        List<AllUserVO> list = pcUserService.getAllUsers(userDto);
        Long total = pcUserService.getCount(userDto);
        Map map = new HashMap();
        map.put("list",list);
        map.put("total",total);
        return ResultGenerator.genSuccessResult(map);
    }

    @ApiOperation(value = "更改用户状态",notes = "0:live;1:dead")
    @PutMapping("/update/{id}")
    public Result updateUserStatus(@PathVariable("id") Long id){
        String s = pcUserService.updateUserStatus(id);
        if(s=="修改成功"){
            return ResultGenerator.genSuccessResult("修改用户状态成功");
        }else{
            return ResultGenerator.genFailResult();
        }

    }

    @ApiOperation(value = "展示用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true,dataType = "Long")
    })

    @GetMapping("/baseMes/{id}")
    public Result getUserBaseMes(@PathVariable("id") long id){
        BaseUserVO userBaseMes = pcUserService.getUserBaseMes(id);
        AccUserVO userAccMes = pcUserService.getUserAccMes(id);
        List<CarUserVO> carMes = pcUserService.getCarMes(id);
        List<OrderUserVO> orderMes = pcUserService.getOrderMes(id);
        List list = new ArrayList();
        list.add(userBaseMes);
        list.add(userAccMes);
        list.add(carMes);
        list.add(orderMes);
        return ResultGenerator.genSuccessResult(list);
    }
//    @ApiOperation(value = "展示用户信息中车辆情况信息")
//    @PostMapping("/getCarMes/{id}")
//    public Result getCarMes(@PathVariable("id") long id,@RequestBody CarDTO carDto){
//        List<CarUserVO> list = pcUserService.getCarMes(carDto);
//        Long total = pcUserService.getCountCar(carDto);
//        Map map = new HashMap();
//        map.put("list",list);
//        map.put("total",total);
//        return ResultGenerator.genSuccessResult(map);
//    }
//    @ApiOperation(value = "订单记录详细信息")
//    @PostMapping("/getOrderMes/{id}")
//    public Result getOrderMes(@PathVariable("id") long id,@RequestBody OrderDTO orderDto){
//        List<OrderUserVO> list = pcUserService.getOrderMes(orderDto);
//        Long total = pcUserService.getCountOrder(orderDto);
//        Map map = new HashMap();
//        map.put("list",list);
//        map.put("total",total);
//        return ResultGenerator.genSuccessResult(map);
//    }

    @ApiOperation(value = "导出泊位信息excel")
    @GetMapping(value = "/excel")
    public ModelAndView exportExcel() {

        UserDTO userDTO = new UserDTO();
        ModelAndView mv = new ModelAndView();
        PageData pd = new PageData();
        try {
            Map<String, Object> dataMap = new HashMap<>();
            List<String> titles = new ArrayList<>();
            titles.add("用户名称"); // 1
            titles.add("绑定手机号"); // 2
            titles.add("绑定车辆数"); // 3
            titles.add("订单数量"); // 4
            titles.add("微信绑定"); // 2
            titles.add("用户状态"); // 3
            titles.add("注册时间"); // 4
            dataMap.put("titles", titles);
            List<AllUserVO> varOList = pcUserService.getAllUserExcel(userDTO);
            List<PageData> varList = new ArrayList<>();
            for (int i = 0; i < varOList.size(); i++) {
                PageData vpd = new PageData();
                vpd.put("var1", varOList.get(i).getUserName()); // 1
                vpd.put("var2", varOList.get(i).getUserTel()); // 2
                vpd.put("var3", varOList.get(i).getAccCarsBound()); // 3
                vpd.put("var4", varOList.get(i).getAccOrderCount()); // 4
                vpd.put("var5", varOList.get(i).getUserIsWechat()); // 2
                vpd.put("var6", varOList.get(i).getUserStatus()); // 3
                vpd.put("var7", varOList.get(i).getUserCreatedTime()); // 4
                varList.add(vpd);
            }
            dataMap.put("varList", varList);
            ObjectExcelView erv = new ObjectExcelView();
            mv = new ModelAndView(erv, dataMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mv;
    }
}
